﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using RoRoWo.Domain;

namespace RoRoWo.Data
{
    public class BlogCategoryRespository
    {
        private ISession _session;
        public ISession Session
        {
            set
            {
                _session = value;
            }
        }

        public BlogCategoryRespository(ISession session)
        {
            _session = session;
        }

        public Blogcategory GetById(int cateId)
        {

            return _session.Get<Blogcategory>(cateId);
        }

        public void Create(Blogcategory dto)
        {
            _session.Save(dto);
            _session.Flush();
        }

        public int CreateAndReturn(Blogcategory dto)
        {
            int newid = (int)_session.Save(dto);
            _session.Flush();
            return newid;
        }

        /// <summary>
        /// 使用事务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public int CreateTransaction(Blogcategory dto)
        {
            using (ITransaction tx = _session.BeginTransaction())
            {
                try
                {
                    int newId = (int)_session.Save(dto);
                    _session.Flush();
                    tx.Commit();
                    return newId;
                }
                catch (HibernateException)
                {
                    tx.Rollback();
                    throw;
                }
            }
        }

        public void UpdateCustomer(Blogcategory dto)
        {
            _session.Update(dto);
            _session.Flush();
        }

        public void SaveOrUpdate(IList<Blogcategory> dtos)
        {
            foreach (var c in dtos)
            {
                _session.SaveOrUpdate(c);
            }
            _session.Flush();
        }

        public void Delete(Blogcategory dto)
        {
            _session.Delete(dto);
            _session.Flush();
        }

        public IList<Blogcategory> From()
        {
            //返回所有Blogcategory类的实例
            return _session.CreateQuery("from Blogcategory")
                .List<Blogcategory>();
        }

        public IList<int> Select()
        {
            //返回所有Blogcategory的CateId
            return _session.CreateQuery("select c.Cateid from Blogcategory c")
                .List<int>();
        }

        public IList<Blogcategory> Where()
        {
            return _session.CreateQuery("from Blogcategory c where c.Cateid='3'")
                .List<Blogcategory>();
        }

        public IList<Blogcategory> GetGreaterThan(int CateId)
        {
            //return _session.CreateQuery("select from Blogcategory c where c.CateId > :cid")
            //    .SetInt32("cid", CateId)
            //    .List<Blogcategory>();

            return _session.CreateCriteria(typeof(Blogcategory))
            .Add(Restrictions.Gt("Cateid", CateId))
            .List<Blogcategory>();

        }

        public IList<Blogcategory> CreateCriteria()
        {
            ICriteria crit = _session.CreateCriteria(typeof(Blogcategory));
            crit.SetMaxResults(50);
            IList<Blogcategory> blogcategorys = crit.List<Blogcategory>();
            return blogcategorys;
        }

        public IList<Blogcategory> Narrowing()
        {
            IList<Blogcategory> blogcategorys = _session.CreateCriteria(typeof(Blogcategory))
                .Add(Restrictions.Like("Catename", "s%"))
                .Add(Restrictions.Between("Parentid", "1", "3"))
                .List<Blogcategory>();
            return blogcategorys;
        }

        public IList<Blogcategory> Query()
        {
            Blogcategory dtoSample = new Blogcategory() { Catename = "sss", Parentid = 0 };
            return _session.CreateCriteria(typeof(Blogcategory))
                .Add(Example.Create(dtoSample))
                .List<Blogcategory>();
        }

        public IList<Blogcategory> UseQueryByExample_Get(Blogcategory dtoSample)
        {
            Example example = Example.Create(dtoSample)
                .IgnoreCase()
                .EnableLike()
                .SetEscapeCharacter('&');
            return _session.CreateCriteria(typeof(Blogcategory))
               .Add(example)
               .List<Blogcategory>();
        }

    }
}
